package com.cuitrip.util;


import com.cuitrip.config.Config;
import com.cuitrip.mode.Commit;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by MiJack on 2015/10/13.
 */
public class TextUtil {
    public static String listToString(List objects) {
        if (objects == null || objects.size() == 0) {
            return "[]";
        }
        StringBuilder sb = new StringBuilder("[");
        for (Object object : objects) {
            sb.append("\n\t").append(object);
        }
        sb.append("\n]");
        return sb.toString();
    }

    public static String mapToString(Map map) {
        if (map == null || map.size() == 0) {
            return "[]";
        }
        StringBuilder sb = new StringBuilder("[");
        for (Object key : map.keySet()) {
            Object value = map.get(key);
            sb.append("\n\t{ key=").append(key).append(",value=").append(value).append("},");
        }
        sb.append("\n]");
        return sb.toString();
    }

    public static boolean isCommit(Config config, String commit) {
        List<Commit> comitModes = config.getComitModes();
        for (Commit mode : comitModes) {
            String t = mode.getMode();
            if ("start-with".equals(t)) {
                if (commit.startsWith(mode.getValue())) {
                    return true;
                }
            }
            if ("start-end".equals(t)) {
                if (commit.endsWith(mode.getValue())) {
                    return true;
                }
            }
            if ("regex".equals(t)) {
                Pattern pattern = Pattern.compile(mode.getValue());
                Matcher matcher = pattern.matcher(commit);
                boolean b = matcher.matches();
                if (b) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isEmpty(String commit) {
        if (commit == null || commit.length() == 0) {
            return true;
        }
        return false;
    }
//    public static boolean isComment(String key) {
//        return key.startsWith("/*")||key.startsWith("/*");
//    }
//
//    public static List<String> getValues(String string) {
//        List<String> list = new ArrayList<>();
//        String s = "";
//        for (int i = 0; i < string.length(); i++) {
//            char c = string.charAt(i);
//            if (c == ',') {
//                list.add(s);
//                s = "";
//            } else {
//                s += c;
//            }
//        }
//        list.add(s);
//        return list;
//    }
//
//    public static String formatiOS(String s) {
//        return s.replace("{INT}","%d").replace("{STRING}","%@").replace("{FLOAT}","%.2f").replace("\n","\\n");
//    }
//
//    public static String formatAndroid(String s) {
//        return s.replace("{INT}","%d").replace("{STRING}","%s").replace("{FLOAT}","%f").replace("\n","\\n");
//    }
}
